home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Sample.bin / Chart.java < prev    next >
Text File  |  1998-09-15  |  10KB  |  313 lines

  1. /*
  2.  * @(#)Chart.java    1.4 97/07/30
  3.  *
  4.  * Copyright (c) 2070, 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
  7.  * modify and redistribute this software in source and binary code form,
  8.  * provided that i) this copyright notice and license appear on all copies of
  9.  * the software; and ii) Licensee does not utilize the software in a manner
  10.  * which is disparaging to Sun.
  11.  *
  12.  * This software is provided "AS IS," without a warranty of any kind. ALL
  13.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
  14.  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
  15.  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
  16.  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  17.  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
  18.  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
  19.  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
  20.  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
  21.  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
  22.  * POSSIBILITY OF SUCH DAMAGES.
  23.  *
  24.  * This software is not designed or intended for use in on-line control of
  25.  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
  26.  * the design, construction, operation or maintenance of any nuclear
  27.  * facility. Licensee represents and warrants that it will not use or
  28.  * redistribute the Software for such purposes.
  29.  */
  30.  
  31. import java.awt.Graphics;
  32. import java.awt.Color;
  33. import java.awt.Font;
  34. import java.awt.FontMetrics;
  35. import java.io.*;
  36. import java.lang.*;
  37. import java.net.URL;
  38.  
  39. public class Chart extends java.applet.Applet {
  40.     static final int    VERTICAL = 0;
  41.     static final int     HORIZONTAL = 1;
  42.  
  43.     static final int    SOLID = 0;
  44.     static final int    STRIPED = 1;
  45.  
  46.     int            orientation;
  47.     String        title;
  48.     Font        titleFont;
  49.     FontMetrics        titleFontMetrics;
  50.     int            titleHeight = 15;
  51.     int            columns;
  52.     int            values[];
  53.     Object        colors[];
  54.     Object        labels[];
  55.     int            styles[];
  56.     int            scale = 10;
  57.     int            maxLabelWidth = 0;
  58.     int            barWidth;
  59.     int            barSpacing = 10;
  60.     int            max = 0;
  61.  
  62.     public synchronized void init() {
  63.     String rs;
  64.     
  65.     titleFont = new java.awt.Font("Courier", Font.BOLD, 12);
  66.     titleFontMetrics = getFontMetrics(titleFont);
  67.     title = getParameter("title");
  68.  
  69.     if (title == null) {
  70.         title = "Chart";
  71.     }
  72.     rs = getParameter("columns");
  73.     if (rs == null) {
  74.         columns = 5;
  75.     } else {
  76.         columns = Integer.parseInt(rs);
  77.     }
  78.     rs = getParameter("scale");
  79.     if (rs == null) {
  80.         scale = 10;
  81.     } else {
  82.         scale = Integer.parseInt(rs);
  83.     }
  84.  
  85.     rs = getParameter("orientation");
  86.     if (rs == null) {
  87.         orientation = VERTICAL;
  88.     } else if (rs.toLowerCase().equals("vertical")) {
  89.         orientation = VERTICAL;
  90.     } else if (rs.toLowerCase().equals("horizontal")) {
  91.         orientation = HORIZONTAL;
  92.     } else {
  93.         orientation = VERTICAL;
  94.     }
  95.     values = new int[columns];
  96.     colors = new Color[columns];
  97.     labels = new String[columns];
  98.     styles = new int[columns];
  99.     for (int i=0; i < columns; i++) {
  100.         // parse the value for this column
  101.         rs = getParameter("C" + (i+1));
  102.         if (rs != null) {
  103.         try {
  104.             values[i] = Integer.parseInt(rs);
  105.         } catch (NumberFormatException e) {
  106.             values[i] = 0;
  107.         }
  108.         }
  109.         if (values[i] > max) {
  110.         max = values[i];
  111.         }
  112.  
  113.         // parse the label for this column
  114.         rs = getParameter("C" + (i+1) + "_label");
  115.         labels[i] = (rs == null) ? "" : rs;
  116.         maxLabelWidth = Math.max(titleFontMetrics.stringWidth((String)(labels[i])),
  117.                      maxLabelWidth);
  118.  
  119.         // parse the bar style
  120.         rs = getParameter("C" + (i+1) + "_style");
  121.         if (rs == null || rs.toLowerCase().equals("solid")) {
  122.         styles[i] = SOLID;
  123.         } else if (rs.toLowerCase().equals("striped")) {
  124.         styles[i] = STRIPED;
  125.         } else {
  126.         styles[i] = SOLID;
  127.         }
  128.         // parse the color attribute for this column
  129.         rs = getParameter("C" + (i+1) + "_color");
  130.         if (rs != null) {
  131.         if (rs.equals("red")) {
  132.             colors[i] = Color.red;
  133.         } else if (rs.equals("green")) {
  134.             colors[i] = Color.green;
  135.         } else if (rs.equals("blue")) {
  136.             colors[i] = Color.blue;
  137.         } else if (rs.equals("pink")) {
  138.             colors[i] = Color.pink;
  139.         } else if (rs.equals("orange")) {
  140.             colors[i] = Color.orange;
  141.         } else if (rs.equals("magenta")) {
  142.             colors[i] = Color.magenta;
  143.         } else if (rs.equals("cyan")) {
  144.             colors[i] = Color.cyan;
  145.         } else if (rs.equals("white")) {
  146.             colors[i] = Color.white;
  147.         } else if (rs.equals("yellow")) {
  148.             colors[i] = Color.yellow;
  149.         } else if (rs.equals("gray")) {
  150.             colors[i] = Color.gray;
  151.         } else if (rs.equals("darkGray")) {
  152.             colors[i] = Color.darkGray;
  153.         } else {
  154.             colors[i] = Color.gray;
  155.         }
  156.         } else {
  157.         colors[i] = Color.gray;
  158.         }
  159.     }
  160.     switch (orientation) {
  161.       case VERTICAL:
  162.       default:
  163.         barWidth = maxLabelWidth;
  164.         resize(Math.max(columns * (barWidth + barSpacing),
  165.                 titleFontMetrics.stringWidth(title)) +
  166.            titleFont.getSize() + 5,
  167.            (max * scale) + (2 * titleFont.getSize()) + 5 + titleFont.getSize());
  168.         break;
  169.       case HORIZONTAL:
  170.         barWidth = titleFont.getSize();
  171.         resize(Math.max((max * scale) + titleFontMetrics.stringWidth("" + max),
  172.                 titleFontMetrics.stringWidth(title)) + maxLabelWidth + 5,
  173.            (columns * (barWidth + barSpacing)) + titleFont.getSize() + 10);
  174.         break;
  175.     }
  176.     }
  177.  
  178.     public synchronized void paint(Graphics g) {
  179.     int i, j;
  180.     int cx, cy;
  181.     char l[] = new char[1];
  182.  
  183.  
  184.     // draw the title centered at the bottom of the bar graph
  185.     g.setColor(Color.black);
  186.     i = titleFontMetrics.stringWidth(title);
  187.     g.setFont(titleFont);
  188.     g.drawString(title, Math.max((getSize().width - i)/2, 0),
  189.              getSize().height - titleFontMetrics.getDescent()); 
  190.     for (i=0; i < columns; i++) {
  191.         switch (orientation) {
  192.           case VERTICAL:
  193.           default:
  194.         // set the next X coordinate to account for the label
  195.         // being wider than the bar getSize().width.
  196.         cx = (Math.max((barWidth + barSpacing),maxLabelWidth) * i) +
  197.             barSpacing;
  198.  
  199.         // center the bar chart
  200.         cx += Math.max((getSize().width - (columns *
  201.                      (barWidth + (2 * barSpacing))))/2,0);
  202.         
  203.         // set the next Y coordinate to account for the getSize().height
  204.         // of the bar as well as the title and labels painted
  205.         // at the bottom of the chart.
  206.         cy = getSize().height - (values[i] * scale) - 1 - (2 * titleFont.getSize());
  207.  
  208.         // draw the label
  209.         g.setColor(Color.black);        
  210.         g.drawString((String)labels[i], cx,
  211.                  getSize().height - titleFont.getSize() - titleFontMetrics.getDescent());
  212.  
  213.         // draw the shadow bar
  214.         if (colors[i] == Color.black) {
  215.             g.setColor(Color.gray);
  216.         }
  217.         g.fillRect(cx + 5, cy - 3, barWidth,  (values[i] * scale));
  218.         // draw the bar with the specified color
  219.         g.setColor((Color)(colors[i]));
  220.         switch (styles[i]) {
  221.           case SOLID:
  222.           default:
  223.             g.fillRect(cx, cy, barWidth, (values[i] * scale));
  224.             break;
  225.           case STRIPED:
  226.             {
  227.             int steps = (values[i] * scale) / 2;
  228.             int ys;
  229.  
  230.             for (j=0; j < steps; j++) {
  231.                 ys = cy + (2 * j);
  232.                 g.drawLine(cx, ys, cx + barWidth, ys);
  233.             }
  234.             }
  235.             break;
  236.         }
  237.         g.drawString("" + values[i],
  238.                  cx,
  239.                  cy - titleFontMetrics.getDescent());
  240.         break;
  241.           case HORIZONTAL:
  242.         // set the Y coordinate
  243.         cy = ((barWidth + barSpacing) * i) + barSpacing;
  244.                    
  245.         // set the X coordinate to be the getSize().width of the widest
  246.         // label 
  247.         cx = maxLabelWidth + 1;
  248.  
  249.         cx += Math.max((getSize().width - (maxLabelWidth + 1 +
  250.                      titleFontMetrics.stringWidth("" +
  251.                                    max) +
  252.                      (max * scale))) / 2, 0);
  253.         // draw the labels and the shadow
  254.         g.setColor(Color.black);        
  255.         g.drawString((String)labels[i], cx - maxLabelWidth - 1,
  256.                  cy + titleFontMetrics.getAscent());
  257.         if (colors[i] == Color.black) {
  258.             g.setColor(Color.gray);
  259.         }
  260.         g.fillRect(cx + 3,
  261.                cy + 5,
  262.                (values[i] * scale),
  263.                barWidth);
  264.  
  265.         // draw the bar in the current color
  266.         g.setColor((Color)(colors[i]));
  267.         switch (styles[i]) {
  268.           case SOLID:
  269.           default:
  270.             g.fillRect(cx,
  271.                    cy,
  272.                    (values[i] * scale),
  273.                    barWidth);
  274.             break;
  275.           case STRIPED:
  276.             {
  277.             int steps = (values[i] * scale) / 2;
  278.             int ys;
  279.  
  280.             for (j=0; j < steps; j++) {
  281.                 ys = cx + (2 * j);
  282.                 g.drawLine(ys, cy, ys, cy + barWidth);
  283.             }
  284.             }
  285.             break;
  286.         }
  287.         g.drawString("" + values[i],
  288.                  cx + (values[i] * scale) + 3,
  289.                  cy + titleFontMetrics.getAscent());
  290.                  
  291.         break;
  292.         }
  293.     }
  294.     }
  295.     public String getAppletInfo() {
  296.     return "Title: Bar Chart \nAuthor: Sami Shaio \nA simple bar chart demo.";
  297.   }
  298.   
  299.   public String[][] getParameterInfo() {
  300.     String[][] info = {
  301.       {"title", "string", "The title of bar graph.  Default is 'Chart'"},
  302.       {"scale", "int", "The scale of the bar graph.  Default is 10."},
  303.       {"columns", "int", "The number of columns/rows.  Default is 5."},
  304.       {"orientation", "{VERTICLE, HORIZONTAL}", "The orienation of the bar graph.  Default is VERTICLE."},
  305.       {"c#", "int", "Subsitute a number for #.  The value/size of bar #.  Default is 0."},
  306.       {"c#_label", "string", "The label for bar #.  Default is no label."},
  307.       {"c#_style", "{SOLID, STRIPED}", "The style of bar #.  Default is SOLID."},
  308.       {"c#_color", "{RED, GREEN, BLUE, PINK, ORANGE, MAGENTA, CYAN, WHITE, YELLOW, GRAY, DARKGRAY}", "The color of bar #.  Default is GRAY."}
  309.     };
  310.     return info;
  311.   }
  312. }
  313.